package com.example.myfr;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import com.example.myfr.framgment.FirstFragment;
import com.example.myfr.framgment.IndexFragment;
import com.example.myfr.framgment.SecondFragment;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener  {
        DrawerLayout mDrawerLayout;
        ActionBarDrawerToggle mToggle;
        LinearLayout mRightContentll;
        NavigationView mNavv;
        FragmentManager fragmentManager=getSupportFragmentManager();
        Fragment mFragment;
        CardView firstCard;
        Class fragmentClass;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer);
        mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.oppo,R.string.vivo);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mRightContentll=findViewById(R.id.rightCotent);
        mNavv=findViewById(R.id.design_navigation_view);
        intNv(mNavv);
        firstCard = findViewById(R.id.firstCard);
        firstCard.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (mToggle.onOptionsItemSelected(item)){
            return true;
        }

        else{
            return super.onOptionsItemSelected(item);
        }
    }
    private void intNv(NavigationView navigationView){
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                mRightContentll.setVisibility(View.INVISIBLE);
                nvItemSelect(menuItem);
                return true;
            }
        });
    }
    private  void  nvItemSelect(MenuItem item){
        switch (item.getItemId()){
            case R.id.item1:{
//                mRightContentll.setVisibility(View.VISIBLE);
                fragmentClass=FirstFragment.class;
                break;
            }
            case R.id.item2:{
              fragmentClass= SecondFragment.class;
              break;
            }
            default:{
                fragmentClass= IndexFragment.class;
                mRightContentll.setVisibility(View.VISIBLE);
                break;
            }
        }
        try {
            if (fragmentClass!=null){
                mFragment=(Fragment)fragmentClass.newInstance();
                fragmentManager.beginTransaction().replace(R.id.rightTitle,mFragment);
            }

            mFragment=(Fragment)fragmentClass.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        fragmentManager.beginTransaction().replace(R.id.rightTitle,mFragment).commit();
        item.setChecked(true);
        setTitle(item.getTitle());
        mDrawerLayout.closeDrawers();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.firstCard:
            {
                Intent intent=new Intent(MainActivity.this,Card1Activity.class);
                intent.putExtra("cardTitle","firstCard");
                startActivity(intent);
                Log.d("wang","tiaozhuan");
                break;
            }
        }
    }
}
